package 剑指;

public class offer06 {
   public class ListNode {
       int val;
       ListNode next;
       ListNode(int x) {
           val = x;
       }
   }
   public int[] reversePrint(ListNode head) {
       if(head == null) return new int[0];
       // count用来计算长度
       int count = 0;
       ListNode pre = head;
       ListNode cur = head.next;
       // 这里是反转链表
       while(cur != null) {
           ListNode curNext = cur.next;
           cur.next = pre;
           pre = cur;
           cur = curNext;
           count++;
       }
       head.next = null;
       // 再次遍历.然后拷贝元素到arr中
       int[] arr = new int[count];
       int index= 0 ;
       while(pre != null){
           arr[index++] = pre.val;
           pre = pre.next;
       }
       return arr;
   }
}

